package com.jade.editor.object.editors.parts;


import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SettingsGrid extends Dialog {
	
	private RGB _rgb = new RGB(204, 204, 204);
	private int _oldIndex = 0;
	private int _pxValue = 0;
	private int _ppi;
	
	public RGB getColor(){
		return _rgb;
	}
	
	public int getDistanse(){
		return _pxValue;
	}
	
	public int getMetrics(){
		return _oldIndex;
	}
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsGrid(Shell parentShell, int dist, RGB oldColor, int oldMetrics) {
		super(parentShell);
		_oldIndex = oldMetrics;
		_rgb = oldColor;
		_ppi = getPPI(parentShell.getShell());
		
		if (oldMetrics == 0){
			_pxValue = dist;
		}
		else if (oldMetrics == 1){
			double mmInpx = 25.6/_ppi; //mm in one px
			double pxTomm = mmInpx * dist;
			double vol = Math.rint(100.0 * pxTomm)/100.0;
			_pxValue = (int) (vol*100.0);
		}
		else if (oldMetrics == 2){
			double cmInpx = 2.56/_ppi; //cm in one px
			double pxTocm = cmInpx * dist;
			double vol = Math.rint(100.0 * pxTocm)/100.0;
			_pxValue = (int) (vol*100.0);
		}
		
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
	
		
		Label distanceLabel = new Label(container, SWT.NONE);
		distanceLabel.setBounds(124, 10, 131, 15);
		distanceLabel.setText("Distance between Lines:");
		
		final Spinner xSpinner = new Spinner(container, SWT.BORDER);
		xSpinner.setBounds(145, 31, 77, 22);
		xSpinner.setMinimum(1);
		xSpinner.setMaximum(999999999);
		
		final Spinner ySpinner = new Spinner(container, SWT.BORDER);
		ySpinner.setEnabled(false);
		ySpinner.setBounds(145, 59, 77, 22);
		ySpinner.setMinimum(1);
		ySpinner.setMaximum(999999999);
		
		if (_oldIndex == 1 || _oldIndex == 2){
			xSpinner.setDigits(2);
			ySpinner.setDigits(2);
			
			if (_oldIndex == 1){
				double mmInpx = 25.6/_ppi; //mm in one px
				xSpinner.setMinimum((int)(mmInpx * 100.0)+1);
			}
			if (_oldIndex == 2){
				double cmInpx = 2.56/_ppi; //cm in one px
				xSpinner.setMinimum((int)(cmInpx * 100.0)+1);
			}
			
		}
		xSpinner.setSelection(_pxValue);
		ySpinner.setSelection(_pxValue);
		
		xSpinner.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
		    	  ySpinner.setSelection(xSpinner.getSelection());
		    	  _pxValue = xSpinner.getSelection();
		      }
		});
		
		Label xLabel = new Label(container, SWT.NONE);
		xLabel.setBounds(129, 34, 16, 15);
		xLabel.setText("X:");
		
		final Combo combo = new Combo(container, SWT.READ_ONLY);
		combo.setBounds(228, 44, 91, 23);
		combo.add("Pixels");
		combo.add("Millimeters");
		combo.add("Centimeters");
		combo.select(_oldIndex);
		combo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int newIndex = combo.getSelectionIndex();
				
				if(_oldIndex == 0 && newIndex == 1){		//px to mm
					int value = xSpinner.getSelection();
					xSpinner.setDigits(2);
					ySpinner.setDigits(2);
					double mmInpx = 25.6/_ppi; //mm in one px
					double pxTomm = mmInpx * value;
					double vol = Math.rint(100.0 * pxTomm)/100.0;
					int res = (int) (vol*100.0);
					xSpinner.setSelection(res);
					ySpinner.setSelection(res);
					xSpinner.setMinimum((int)(mmInpx * 100.0)+1);
				}
				else if(_oldIndex == 0 && newIndex == 2){	//px to cm
					int value = xSpinner.getSelection();
					xSpinner.setDigits(2);
					ySpinner.setDigits(2);
					double cmInpx = 2.56/_ppi; //cm in one px
					double pxTocm = cmInpx * value;
					double vol = Math.rint(100.0 * pxTocm)/100.0;
					int res = (int) (vol*100.0);
					xSpinner.setSelection(res);
					ySpinner.setSelection(res);
					xSpinner.setMinimum((int)(cmInpx * 100.0)+1);
					
				}
				else if(_oldIndex == 1 && newIndex == 0){	//mm to px
					double value = xSpinner.getSelection() / 100.0;
					xSpinner.setDigits(0);
					ySpinner.setDigits(0);
					double pxInmm = _ppi/25.6;	//px in one mm
					int mmTopx = (int) (pxInmm * value);
					xSpinner.setSelection(mmTopx);
					ySpinner.setSelection(mmTopx);
					xSpinner.setMinimum(1);
					
				}
				else if(_oldIndex == 1 && newIndex == 2){	//mm to cm
					double value = xSpinner.getSelection() / 10.0;
					int mmTocm = (int) value;
					xSpinner.setSelection(mmTocm);
					ySpinner.setSelection(mmTocm);
					double cmInpx = 2.56/_ppi; //cm in one px
					xSpinner.setMinimum((int)(cmInpx * 100.0)+1);
				}
				else if(_oldIndex == 2 && newIndex == 0){	//cm to px
					double value = xSpinner.getSelection() / 100.0;
					xSpinner.setDigits(0);
					ySpinner.setDigits(0);
					double pxInmm = _ppi/2.56;	//px in one cm
					int mmTopx = (int) (pxInmm * value);
					xSpinner.setSelection(mmTopx);
					ySpinner.setSelection(mmTopx);
					xSpinner.setMinimum(1);
					
				}
				else if(_oldIndex == 2 && newIndex == 1){	//cm to mm
					double value = xSpinner.getSelection() * 10.0;
					int mmTocm = (int) value;
					xSpinner.setSelection(mmTocm);
					ySpinner.setSelection(mmTocm);
					double mmInpx = 25.6/_ppi; //cm in one px
					xSpinner.setMinimum((int)(mmInpx * 100.0)+1);
				}
				

		    	_oldIndex = combo.getSelectionIndex();
		    	_pxValue = xSpinner.getSelection();
		      }
		});
		

		
		Label yLabel = new Label(container, SWT.NONE);
		yLabel.setBounds(129, 64, 20, 15);
		yLabel.setText("Y:");
		
		final Composite colorComposite = new Composite(container, SWT.BORDER);
		colorComposite.setBackground(SWTResourceManager.getColor(_rgb));
		colorComposite.setBounds(10, 113, 424, 98);
		
		final Label clickMeLabel = new Label(colorComposite, SWT.NONE);
		clickMeLabel.setBackground(SWTResourceManager.getColor(_rgb));
		clickMeLabel.setBounds(0, 79, 121, 15);
		clickMeLabel.setText("Click to choose color");
		
		final Label linesColorLabel = new Label(container, SWT.NONE);
		linesColorLabel.setBounds(10, 92, 204, 15);
		linesColorLabel.setText("Lines color: " + _rgb.toString());
		
		
		colorComposite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ColorDialog cd = new ColorDialog(container.getShell());
				RGB tmpRGB = _rgb;
				_rgb = cd.open();
				if (_rgb != null){
					colorComposite.setBackground(SWTResourceManager.getColor(_rgb));
					linesColorLabel.setText("Lines color: " + _rgb.toString());
					clickMeLabel.setBackground(SWTResourceManager.getColor(_rgb));
				}
				else
					_rgb = tmpRGB;
			}
		});
		
		
		
		return container;
	}
	
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Grid Settings");
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Button okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if (_oldIndex == 1){	//mm to px
					double value = _pxValue / 100.0;
					double pxInmm = _ppi/25.6;
					_pxValue = (int) (pxInmm * value);
				}
				else if (_oldIndex == 2){	//cm to px
					double value = _pxValue / 100.0;
					double pxInmm = _ppi/2.56;
					_pxValue = (int) (pxInmm * value);
				}
			}
		});
	}

	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	private int getPPI(Shell shell){
		Device device = shell.getDisplay();
		return device.getDPI().x;
	}
}
